<!DOCTYPE html>
<html lang="en">
<head>
    <title>药物管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="demoTable layui-form">
    名称：
    <div class="layui-inline">
        <select name="name" id="name" lay-search="">
            <option value="">请选择</option>
        </select>
    </div>
    类型：
    <div class="layui-inline">
        <select name="drugType" id="drugTypesearch" lay-search="">
            <option value="">请选择</option>
        </select>
    </div>
    单位：
    <div class="layui-inline">
        <select name="unit" id="unitsearch" lay-search="">
            <option value="">请选择</option>
        </select>
    </div>
    规范：
    <div class="layui-inline">
        <select name="specification" id="specificationsearch" lay-search="">
            <option value="">请选择</option>
        </select>
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<!-- 自定义的控件 -->
<div id="toolbar" style="display: none; ">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add" data-method="notice" data-type="auto">添加</button>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="stock">进货</a>
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>
<div id="modal" style="display:none">
    <form class="layui-form" id="user-add" action="" lay-filter="entity">
        <input type="text" name="id" id="id" style="display:none">
        <div class="layui-form-item">
            <label class="layui-form-label">药物名称：</label>
            <div class="layui-input-inline layui-col-md4">
                <select name="name" id="typeName" lay-search="">
                    <option value="" obj="{xxx,xx,xxx,xxx,xxx}">直接选择或搜索选择</option>
                </select>
            </div>
            <label class="layui-form-label">药物类型：</label>
            <div class="layui-input-inline layui-col-md4">
                <select name="drugType" id="drugType" lay-search="">
                    <option value="" obj="{xxx,xx,xxx,xxx,xxx}">直接选择或搜索选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">药物单位：</label>
            <div class="layui-input-inline layui-col-md4">
                <select name="unit" id="unit" lay-search="">
                    <option value="" obj="{xxx,xx,xxx,xxx,xxx}">直接选择或搜索选择</option>
                </select>
            </div>
            <label class="layui-form-label">药物规范：</label>
            <div class="layui-input-inline layui-col-md4">
                <select name="specification" id="specification" lay-search="">
                    <option value="" obj="{xxx,xx,xxx,xxx,xxx}">直接选择或搜索选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">功效分类：</label>
            <div class="layui-input-inline layui-col-md4">
                <select name="efficacyClassification" id="efficacyClassification" lay-search="">
                    <option value="" obj="{xxx,xx,xxx,xxx,xxx}">直接选择或搜索选择</option>
                </select>
            </div>
            <label class="layui-form-label">药物价格：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="price" required lay-verify="required" placeholder="药物价格"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">药品库存：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="drugCount" required lay-verify="required" placeholder="药品库存"
                       autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">是否限制药：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="radio" name="limitStatus" value="0" title="是" checked="">
                <input type="radio" name="limitStatus" value="1" title="否">
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">药品生产日期：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="productionDate" required lay-verify="required" placeholder="药品生产日期"
                       autocomplete="off" class="layui-input laydate">
            </div>
            <label class="layui-form-label">有效期：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="qualityDate" required lay-verify="required" placeholder="药物名称"
                       autocomplete="off" class="layui-input laydate">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">制造商：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="manufacturer" required lay-verify="required" placeholder="制造商"
                       autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">批发价：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="wholesalePrice" required lay-verify="required" placeholder="批发商的价格"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="phone" required lay-verify="required" placeholder="手机号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button style="display: none;" type="reset" class="layui-btn layui-btn-primary swiper-right-form-reset">
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<script src="/js/jquery-3.3.1.min.js" charset="utf-8"></script>
<script src="/layui/layui.js" charset="utf-8"></script>

<script>
    var param;
    layui.use(['table', 'layer', 'form', 'jquery', 'laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        laydate.render({
            elem: '.laydate' //指定元素
        });

        //药物类型
        $.get("/api/dict/getDict?typeNo=drug_type", function (data) {
            $.each(data.data, function (index, item) {
                $('#drugType').append(new Option(item.name, item.name));// 下拉菜单里添加元素
                $('#drugTypesearch').append(new Option(item.name, item.name));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }, "json");
        //药物名称
        $.get("/api/dict/getDict?typeNo=drug_name", function (data) {
            $.each(data.data, function (index, item) {
                $('#typeName').append(new Option(item.name, item.name));// 下拉菜单里添加元素
                $('#name').append(new Option(item.name, item.name));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }, "json");
        //药物功效
        $.get("/api/dict/getDict?typeNo=efficacy_classification", function (data) {
            $.each(data.data, function (index, item) {
                $('#efficacyClassification').append(new Option(item.name, item.name));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }, "json");
//药物规范
        $.get("/api/dict/getDict?typeNo=type_regulation", function (data) {
            $.each(data.data, function (index, item) {
                $('#specification').append(new Option(item.name, item.name));// 下拉菜单里添加元素
                $('#specificationsearch').append(new Option(item.name, item.name));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }, "json");
        //药物单位
        $.get("/api/dict/getDict?typeNo=drug_unit", function (data) {
            $.each(data.data, function (index, item) {
                $('#unit').append(new Option(item.name, item.name));// 下拉菜单里添加元素
                $('#unitsearch').append(new Option(item.name, item.name));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }, "json");

        table.render({
            elem: '#test'
            , url: '/api/drug/page'
            // , height: document.body.clientHeight - 180
            , page: true //开启分页
            , id: 'testReload'
            , cols: [[
                {field: "id", title: "ID"},
                {field: "name", title: "名称"},
                {field: "drugType", title: "类型"},
                {field: "unit", title: "单位"},
                {field: "specification", title: "规范"},
                {field: "efficacyClassification", title: "功效分类"},
                {field: "price", title: "价格"},
                {field: "drugCount", title: "库存"},
                {field: "productionDate", title: "生产日期"},
                {field: "qualityDate", title: "有效期"},
                {
                    field: "limitStatus", title: "是否限制药", templet: function (d) {
                        return d.limitStatus === 0 ? "是" : "否"
                    }
                },
                /*{field: "manufacturer", title: "制造商"},
                {field: "wholesalePrice", title: "批发价"},
                {field: "phone", title: "手机号"},
                {
                    field: "createDatetime",
                    title: "创建时间",
                    templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                },*/
                {title: '操作', width: 200, toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar',//表格头信息
            response: {	// 设置响应码
                statusCode: 1 //规定成功的状态码，默认：0
            },
            parseData: function (rs) {//数据格式解析
                if (rs.status == 1) {
                    return {
                        "code": rs.status,
                        "msg": rs.message,
                        "count": rs.total,
                        "data": rs.data
                    }
                }
            }
        });
        //监听事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    //重载实例
                    open2("添加页面", null);
                    break;
            }
            ;
        });
        //监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除该条信息吗？', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/api/drug/" + data.id,
                        dataType: "json",
                        success: function (da) {
                            if (da.status == 1) {
                                layer.msg(da.message)
                                obj.del(); //删除对应行（tr）的DOM结构
                            } else {
                                layer.msg(da.message)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }); //ajax结束
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                open2("修改页面", obj.data);
            } else if (obj.event === 'stock') {
                param = obj.data
                open(param.name + "进货", "/view/admin/stockAdd.html");
            }
        });

        function open(title, url) {
            layer.open({
                type: 2
                , title: title
                , content: url
                , offset: '50px'
                , area: ['500px', '60%']
                , end: function () {
                    table.reload('testReload');
                }
            });
        }

        //"模态框",
        function open2(title, entity) {
            form.val("entity", entity);
            layer.open({
                type: 1
                , title: title
                , content: $("#modal")
                , offset: '10px'
                , area: ['800px', '95%']
                , end: function () {
                    $('.swiper-right-form-reset').click()
                    table.reload('testReload');
                }
            });
        }

        //监听提交
        form.on('submit(formDemo)', function (data) {
            let data1 = form.val("entity");
            var type = data1.id ? "put" : "post";
            $.ajax({
                url: '/api/drug',
                type: type,
                dataType: "json",
                contentType: 'application/json',
                data: JSON.stringify(data1),
                success: function (da) {
                    if (da.status == 1) {
                        layer.alert(da.message, function () {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(da.message);
                    }
                }
            });
            return false;
        });

        var $ = layui.$, active = {
            reload: function () {
                var name = $('#name');
                var drugTypesearch = $('#drugTypesearch');
                var unitsearch = $('#unitsearch');
                var specificationsearch = $('#specificationsearch');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "name": name.val(),
                        "drugType": drugTypesearch.val(),
                        "unit": unitsearch.val(),
                        "specification": specificationsearch.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>